$(document).ready(function () {
    let rows=30
    let cell=50

    for(let rowIndex=0; rowIndex<rows; rowIndex++){
        let rowDiv=$("<div>")
        rowDiv.addClass("rowDiv")
        for(let cellIndex=0; cellIndex < cell; cellIndex++){
            let cellDiv=$("<div>")
            cellDiv.css({
                "height":"20px",
                "width":"20px",
                "border":"1px #ffffff solid",
                "background-color":"red"
           })

           cellDiv.addClass("row_"+rowIndex)
           cellDiv.addClass("cell_"+cellIndex)

           rowDiv.append(cellDiv)
        }
        $("#plate").append(rowDiv)
    }
    let player={
        nikeName:"极道帝龙",
        // 行驶速度（毫秒）
        speed:500,
        // 定义头部/行进方向
        head:"R",
        position:[
            {
                x:29,
                y:0
            },
            {
                x:29,
                y:1,
            },
            {
                x:29,
                y:2
            },
            
        ],
        color:"123123"
    }
    player.position.forEach(p=>{
        console.log(p);
        // let cell=$(".row_"+value.x+".cell_"+value.y)
        // console.log(cell);
        // cell.css({
        //     "background-color":"#"+player.color
        // })
        $(".row_"+p.x+".cell_"+p.y).css("background-color","#"+player.color)
        
    })


    // 使用定时器，完成蛇的移动
    // setInterval 是异步的，蛇的身体各自计算
    // setTimeout 是同步的，蛇的身体整体计算，但是setTimeout是一次性的，所以这里用到的递归写法
    // 递归函数：（菲官方术语）自己调自己，形成一个循环调用


    // 递归移动：移动
    function move(){
        console.log("我move了");

        setTimeout(()=>{
            console.log("我执行了");
            // 执行移动代码
            // 蛇头所在的位置

            let headCellIndex = player.position
            [player.position.length-1]
            console.log(headCellIndex);

            let nextCellIndex
            switch (player.head) {
                case "U":
                    
                  nextCellIndex={
                    x:headCellIndex.x-1,
                    y:headCellIndex.y
                  }
                    break;
                    case "R":
                    
                  nextCellIndex={
                    x:headCellIndex.x,
                    y:headCellIndex.y+1
                  }
                    break;
                    case "D":
                    
                  nextCellIndex={
                    x:headCellIndex.x+1,
                    y:headCellIndex.y
                  }
                    break;
                    case "L":
                    
                  nextCellIndex={
                    x:headCellIndex.x,
                    y:headCellIndex.y-1
                  }
                    break;
            }
            //判定x 到-1格 或 跑到最后一个之外
            if(nextCellIndex.x<0||nextCellIndex.x+1>rows){
              alert("游戏结束")
            }
             //判断y 到-1格 或 跑到最后一个之外
             else if (nextCellIndex.y < 0 || nextCellIndex.y + 1 > cell) {
              alert("游戏结束")
          }
          else {
            player.position.push(nextCellIndex)
            console.log("nextCellIndex", nextCellIndex);
            let lastIndex = player.position.splice(0, 1)
            console.log("lastIndex", lastIndex);

            $(".row_" + nextCellIndex.x + ".cell_" + nextCellIndex.y).css("background-color", "#" + player.color)
            $(".row_" + lastIndex[0].x + ".cell_" + lastIndex[0].y).css("background-color", "")
            move();
        }
        },player.speed)
    }
    move()
    $("body").keydown(function(e){
      switch (e.keyCode) {
        case 37: //左
          if(player.head =="R")
              return
          player.head="L"
          break;
        case 38: //上
          if(player.head =="D")
              return
          player.head="U"
          break;
        case 39: //右
          if(player.head =="L")
              return
          player.head="R"
          break;
        case 40: //下
          if(player.head =="U")
              return
          player.head="D"
          break;
      }
    })

    setInterval(()=>{
      let newSpeed=player.speed-100
      if(newSpeed<=50){
        player.speed=50
      }
      else(
        player.speed=newSpeed
      )
      
    },1*60*1000)
});